/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:SaleReportJob.java
 * 模块说明：	
 * 修改历史：
 * 2017年11月8日 - liyang - 创建。
 *
 */
package com.thor.palmmall.dao.rpt.sales;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.jpa.entity.POperateInfo;
import com.thor.palmmall.api.rpt.Report;
import com.thor.palmmall.api.rpt.ReportItem;
import com.thor.palmmall.api.rpt.sales.DailySalesReportParam;
import com.thor.palmmall.api.rpt.sales.SalesRptService;
import com.thor.util.ApplicationContextUtils;

/**
 * @author:liyang
 * @time:2017年11月8日 下午5:09:52
 * @since:1.0
 */
public class SaleReportJob extends QuartzJobBean {

  @Override
  protected void executeInternal(JobExecutionContext paramJobExecutionContext)
      throws JobExecutionException {
    List<String> storeList = getSaleReportDao().queryAllStoreIds();
    if (storeList == null || storeList.isEmpty()) {
      return;
    }
    for (String storeId : storeList) {
      handleMonthSaleReport(storeId);
    }
  }

  /**
   * 生成月报表数据
   * 
   * @param storeId
   */
  private void handleMonthSaleReport(String storeId) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
    calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
    Date updateToDate = calendar.getTime();

    Date lastUpdateDate = getSaleReportDao().getLastestReportByStoreId(storeId);
    if (lastUpdateDate == null) {
      lastUpdateDate = getSaleReportDao().getEarliestReportByStoreId(storeId);
    } else {
      // 取历史报表查询月分最大时间查询,这样就不会重复出现某个月销售数据重复的情况
      Calendar aCalendar = Calendar.getInstance();
      aCalendar.setTime(lastUpdateDate);
      aCalendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      aCalendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
      aCalendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
      aCalendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
      lastUpdateDate = aCalendar.getTime();
    }
    DailySalesReportParam lastNMonthSalesAmountParam = new DailySalesReportParam();
    lastNMonthSalesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    lastNMonthSalesAmountParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SALESDATE);
    lastNMonthSalesAmountParam.setBeginDate(lastUpdateDate);
    lastNMonthSalesAmountParam.setEndDate(updateToDate);
    lastNMonthSalesAmountParam.setStoreId(storeId);
    try {
      Report report = getSalesRptService().getFloorMonthSalesReports(lastNMonthSalesAmountParam);
      for (ReportItem item : report.getItems()) {
        PMonthSalesReport monthReport = new PMonthSalesReport();
        monthReport.setAmount(item.getValue());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
        monthReport.setDate(fmt.parse(item.getDimension()));
        monthReport.setStoreId(storeId);
        monthReport.setFloor(item.getValue2());
        POperateInfo operater = new POperateInfo();
        operater.setTime(new Date());
        monthReport.setLastModifyInfo(operater);
        getSaleReportDao().saveSalesReport(monthReport);
      }
    } catch (ThorServiceException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  private SalesRptDao getSaleReportDao() {
    return ApplicationContextUtils.getBean(SalesRptDao.class);
  }

  private SalesRptService getSalesRptService() {
    return ApplicationContextUtils.getBean(SalesRptService.class);
  }
}
